package services;

import dao.KeyWordJpaController;
import entities.KeyWord;
import java.util.List;

public class KeyWordService {
   private static KeyWordJpaController kc;

   private KeyWordJpaController getJPAController()
   {
       if(kc == null)
           kc = new KeyWordJpaController();
       return kc;
   }

   public boolean saveKeyWord(String name)
   {
       boolean ret = true;
       kc = getJPAController();
       try
       {
           kc.create(new KeyWord(0,name));
       }
       catch (Exception e)
       {
           ret = false;
       }
       return ret;
   }
   public KeyWord getKeyWord(String name)
   {
       kc = getJPAController();
       return kc.getKeyWordByName(name);
   }

   public boolean editKeyWord(KeyWord k)
   {
       kc = getJPAController();
       try
       {
           kc.edit(k);
           return true;
       } catch (Exception e)
       {
           return false;
       }
   }

   public boolean deleteKeyWord(KeyWord k)
   {
       kc = getJPAController();
       try
       {
           kc.destroy(k.getIdKeyWord());
           return true;
       }
       catch (Exception e)
       {
           return false;
       }
   }

    public List<KeyWord> getAllKeyWords()
    {
       kc = getJPAController();
       return kc.findAll();
    }

    public KeyWord getKeyWord(Integer idKeyWord)
    {
       kc = getJPAController();
       return kc.findKeyWord(idKeyWord);
    }
}